package com.zzzzzz.oms.receipt;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.WebUtils;

import com.zzzzzz.core.datagrid.BaseData;
import com.zzzzzz.core.datagrid.BaseQueryForm;
import com.zzzzzz.utils.page.Page;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zzzzzz.oms.GCS;
import com.zzzzzz.oms.driver.Driver;
import com.zzzzzz.oms.legs.Legs;
import com.zzzzzz.oms.legs.LegsDao;
import com.zzzzzz.oms.orderHead.OrderHeadDao;
import com.zzzzzz.oms.timeLimit.TimeLimit;
import com.zzzzzz.plugins.poi.ExportExcel;
import com.zzzzzz.plugins.shiro.ShiroUtils;

/**
 * @author hing
 * @version 1.0.0
 */
@Controller
public class ReceiptWeb {

	@Resource
	public ReceiptService receiptService;
	
	@Resource
	public ReceiptDao receiptDao;
	
	@Resource
	private OrderHeadDao orderHeadDao;
	
	@Resource
	private LegsDao legsDao;
	
	// 打开回单界面
	@RequestMapping(value = "/receipt/list", method = RequestMethod.GET)
	public String list() {
		return "oms/receipt";
	}
	
	// 查找一条数据
	@RequestMapping(value = "/receipt/id/{id}", method = RequestMethod.GET)
	@ResponseBody 
	public BaseData findById(@PathVariable Long id) {
		Receipt receipt = receiptDao.findById(id);
		return new BaseData(receipt, null);
	}
	
	// 延时单
	@RequestMapping(value = "/receipt/updReasonById", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updReasonByIds(@RequestParam(value = "id", required = true) Long id,@RequestParam(value = "reason", required = true) String reason) throws Exception {
		receiptDao.updReasonById(id, reason, ShiroUtils.findUser());
		return new BaseData("0");
	}	
	
	// 动态查询
	@RequestMapping(value = "/receipt/findListBy", method = RequestMethod.POST)
	@ResponseBody
	public BaseData findListBy(@RequestBody BaseQueryForm baseQueryForm) {
		Page page=baseQueryForm.getPage();
		List<Receipt> list = receiptDao.findListBy(page,baseQueryForm.getFfMap(),ShiroUtils.findUser());
		BaseData baseData=new BaseData(list, page.getTotal());
		return baseData;
	}
	
	//整标记不合格订单
	@RequestMapping(value = "/receipt/updStByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updStByIds(@RequestParam(value = "ids", required = true) List<Long> ids,@RequestParam(value = "status", required = true) String status) throws Exception {
		receiptDao.updStByIds(ids,status, ShiroUtils.findUser());
		return new BaseData("0");
	}	
	
	//整单签收
	@RequestMapping(value = "/receipt/updByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData updByIds(@RequestParam(value = "ids", required = true) List<Long> ids, @RequestParam(value = "receiptDt", required = true) String receiptDt) throws Exception {
		receiptDao.updByIds(ids, receiptDt, ShiroUtils.findUser());
		return new BaseData("0");
	}	  
	
	//返还中外运确认
	@RequestMapping(value = "/receipt/chageStByIds", method = RequestMethod.POST)
	@ResponseBody
	public BaseData chageStByIds(@RequestParam(value = "ids", required = true) List<Long> ids,@RequestParam(value = "backDt", required = true) String backDt) throws Exception {
		receiptDao.chageStByIds(ids, backDt, ShiroUtils.findUser());
		return new BaseData("0");
	}	
	
	//修改应回单时间
	@RequestMapping(value = "/receipt/changeMustDt", method = RequestMethod.POST)
	@ResponseBody
	public BaseData changeMustDt(@RequestParam(value = "ids", required = true) List<Long> ids,@RequestParam(value = "mustDt", required = true) String mustDt) throws Exception {
		receiptDao.changeMustDt(ids, mustDt, ShiroUtils.findUser());
		return new BaseData("0");
	}
    
	//撤销回单
	@RequestMapping(value = "/receipt/cancelRcp", method = RequestMethod.POST)
	@ResponseBody
	public BaseData cancelRcp(@RequestParam(value = "ids", required = true) List<Long> ids) throws Exception {
		receiptDao.cancelRcp(ids, ShiroUtils.findUser());
		return new BaseData("0");
	}	
  
	//导出
	@RequestMapping(value = "/receipt/export")
    public String exportFileTemplate(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
			Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
            String fileName = "回单数据.xlsx";           
            List<Receipt> list =receiptDao.findListBy(null, params,ShiroUtils.findUser());  
    		new ExportExcel("回单数据", Receipt.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			redirectAttributes.addFlashAttribute("msg", "回单数据导出失败。");
			logger.warn("importFileTemplate", e);
		}
		return "redirect:/msg";
    }
    private static final Logger logger = LoggerFactory.getLogger(ReceiptWeb.class);
}
